package com.qqtech.qquan.vote.service;

import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.google.common.base.Splitter;
import com.google.common.collect.Lists;
import com.qqtech.core.frame.dao.BaseDao;
import com.qqtech.core.frame.service.BaseServiceImpl;
import com.qqtech.qquan.circle.enums.CircleTopicBizzTypeDictEnum;
import com.qqtech.qquan.circle.model.CircleTopic;
import com.qqtech.qquan.circle.service.CircleTopicService;
import com.qqtech.qquan.vote.dao.VoteBaseDao;
import com.qqtech.qquan.vote.dao.VoteItemDao;
import com.qqtech.qquan.vote.enums.VoteBaseBizzTypeDictEnum;
import com.qqtech.qquan.vote.model.VoteBase;
import com.qqtech.qquan.vote.model.VoteBaseVo;
import com.qqtech.qquan.vote.model.VoteItem;

@Service
public class VoteBaseServiceImpl extends BaseServiceImpl<VoteBase> implements VoteBaseService {
	public static final Logger log = LoggerFactory.getLogger(VoteBaseServiceImpl.class);

	@Resource
	private CircleTopicService circleTopicService;

	@Resource
	private VoteItemDao voteItemDao;

	@Resource
	private VoteBaseDao voteBaseDao;

	@Override
	protected BaseDao<VoteBase> getBaseDao() {
		return voteBaseDao;
	}

	@Override
	public int addVoteBase(VoteBaseVo voteBaseVo) {
		Integer voteId = voteBaseDao.insert(voteBaseVo);
		if (VoteBaseBizzTypeDictEnum.CIRCLE.getCode() == voteBaseVo.getBizzType()) {// 圈子话题投票
			CircleTopic circleTopic = new CircleTopic();
			circleTopic.setCircleId(voteBaseVo.getCircleId());
			circleTopic.setCreatorId(voteBaseVo.getCreatorId());
			circleTopic.setContent(voteBaseVo.getContent());
			circleTopic.setImg(voteBaseVo.getImg());
			circleTopic.setBizzId(voteId);
			circleTopic.setBizzType(CircleTopicBizzTypeDictEnum.VOTE.getCode());
			circleTopicService.addTopic(circleTopic);
		}
		Iterator<String> remarks = Splitter.on(";").trimResults().omitEmptyStrings().split(voteBaseVo.getItemRemark())
				.iterator();
		List<String> remarkList = Lists.newArrayList(remarks);
		
		Iterator<String> imgs = Splitter.on(";").trimResults().omitEmptyStrings().split(voteBaseVo.getItemImgs())
				.iterator();
		List<String> imgList = Lists.newArrayList(imgs);
		String remark = "";
		String img = "";
		for (int i = 0; i < remarkList.size(); i++) {
			VoteItem voteItem = new VoteItem();
			voteItem.setVoteId(voteId);
			remark = remarkList.get(i);
			if (null != imgList && imgList.size() > 0) {
				img = imgList.get(i);
			}
			voteItem.setRemark(remark);
			voteItem.setImg(img);
			voteItemDao.insert(voteItem);
		}

		return voteId;
	}

}